/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
  // 指针
  let max1 = l1.length;
  let max2 = l2.length;
  let maxLen = max1 > max2 ? max1 : max2;
  let i = 0;
  let j = 0;
  let final = [];
  let lastStepOn = 0;
  while(i < maxLen || j < maxLen) {
    let A = l1[i] || 0;   // 位置上不存在的补0
    let B = l2[j] || 0;
    let C = A + B;
    let all = C + lastStepOn;
    let mode = all % 10;
    final.push(mode);
    lastStepOn = Math.ceil(all / 10);
    i++;
    j++;
  }
  if(lastStepOn) {
    final.push(lastStepOn);
  }
  return final;
};

console.log(addTwoNumbers([123], [456]));